<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LoginAction
 *
 * @author Magno
 */
class LoginAction extends BaseAction{

    function __construct() {       
        parent::__construct();
    }

    public function login(){
        $ok = false;

        $usuarioService = new UsuarioService();

        $login = parent::getValueInput('login');
        $senha = parent::getValueInput('senha');

        $usuarios = $usuarioService->listar(0, "", $login);

        $usuarioBean = null;

        foreach ($usuarios as $usuario) {
            if(Seguranca::testaSenha($senha,$usuario->getSenha()) === TRUE){
                $ok = true;
                $usuarioBean = $usuario;
                parent::setUserSession($usuarioBean);
                break;
            }
        }
                   
        if($ok){
            $url = "";
            if($usuarioBean->getTipo() == Constantes::$USUARIO_ADMIN_GERAL ||
                   $usuarioBean->getTipo() == Constantes::$USUARIO_ADMIN_SIMPLES)
                   $url = "index2.php?url=AdministracaoAction.principal";
            else if($usuarioBean->getTipo() == Constantes::$USUARIO_CONCURSO)
                   $url = "index2.php?url=AdministracaoAction.concurso";
            else
                   $url = "index2.php?url=IndexAction.index";

            parent::redirect ($url);
        }else{
           parent::setValueOutput("msg", new MensagemMeta(Constantes::$MSG_ERROR, ('Usuário ou Senha Inválidos!!!!')));
           parent::show(AdministracaoAction::$VIEW_ADMIN."index.tpl");
        }
               
    }

    public function logout(){
        parent::setUserSession(null);
        parent::redirect('index2.php?url=AdministracaoAction.index');
    }

}
?>
